namespace CIS
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class UserEdit : Page
    {
        protected Button areaAddBtn;
        protected Button AreaAddBtn;
        protected DropDownList AreaDdl;
        protected Button AreaDeleteBtn;
        protected ListBox AreaLB;
        protected Button catAddBtn;
        protected DropDownList catDdl;
        protected Button catDeleteBtn;
        protected ListBox catLB;
        protected CheckBox isAdminChk;
        protected CheckBox isDownloadChk;
        protected CheckBox isSuperAdminChk;
        protected Label lbArea;
        protected Label lbIsAdmin;
        protected Label lbisDownload;
        protected Label lbIsSuperAdmin;
        protected Label lbloginName;
        protected Label lbprovince;
        protected Label lbRole;
        protected Label lbuserEmail;
        protected Label lbuserName;
        protected TextBox loginNameTbx;
        protected UserPrincipal Principal = null;
        protected Button provinceAddBtn;
        protected DropDownList provinceDdl;
        protected Button provinceDeleteBtn;
        protected ListBox provinceLB;
        protected DropDownList roleDdl;
        protected Button SaveBtn;
        protected TextBox userEmailTbx;
        protected TextBox userNameTbx;

        private void AreaAddBtn_Click(object sender, EventArgs e)
        {
            ListItem tmpItem = new ListItem();
            tmpItem.Text = this.AreaDdl.SelectedItem.Text;
            tmpItem.Value = this.AreaDdl.SelectedItem.Value;
            if (!this.IsExistItem(tmpItem, this.AreaLB))
            {
                this.AreaLB.Items.Add(tmpItem);
            }
        }

        private void AreaDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.provinceDdl.DataSource = ManageProvince.GetProvince(new Guid(this.AreaDdl.SelectedValue));
            this.provinceDdl.DataTextField = "RegionName";
            this.provinceDdl.DataValueField = "RegionId";
            this.provinceDdl.DataBind();
            this.catDdl.DataSource = ManageCAT.GetCAT(new Guid(this.AreaDdl.SelectedValue));
            this.catDdl.DataTextField = "CATGroupName";
            this.catDdl.DataValueField = "CATGroupId";
            this.catDdl.DataBind();
            this.roleDdl.DataSource = ManageRole.GetRole(new Guid(this.AreaDdl.SelectedValue));
            this.roleDdl.DataTextField = "RoleName";
            this.roleDdl.DataValueField = "RoleId";
            this.roleDdl.DataBind();
        }

        private void AreaDeleteBtn_Click(object sender, EventArgs e)
        {
            try
            {
                this.AreaLB.Items.Remove(this.AreaLB.Items[this.AreaLB.SelectedIndex]);
            }
            catch
            {
            }
        }

        private void catAddBtn_Click(object sender, EventArgs e)
        {
            if (this.catDdl.SelectedItem.Value == "00000000-0000-0000-0000-000000000000")
            {
                this.catLB.DataSource = ManageCAT.GetCAT(new Guid(this.AreaDdl.SelectedValue));
                this.catLB.DataTextField = "CATGroupName";
                this.catLB.DataValueField = "CATGroupId";
                this.catLB.DataBind();
                try
                {
                    this.catLB.Items.Remove(this.catDdl.Items[0]);
                }
                catch
                {
                }
            }
            else
            {
                ListItem tmpItem = new ListItem();
                tmpItem.Text = this.catDdl.SelectedItem.Text;
                tmpItem.Value = this.catDdl.SelectedItem.Value;
                if (!this.IsExistItem(tmpItem, this.catLB))
                {
                    this.catLB.Items.Add(tmpItem);
                }
            }
        }

        private void catDeleteBtn_Click(object sender, EventArgs e)
        {
            try
            {
                this.catLB.Items.Remove(this.catLB.Items[this.catLB.SelectedIndex]);
            }
            catch
            {
            }
        }

        private void InitializeComponent()
        {
            this.AreaDdl.SelectedIndexChanged += new EventHandler(this.AreaDdl_SelectedIndexChanged);
            this.AreaAddBtn.Click += new EventHandler(this.AreaAddBtn_Click);
            this.AreaDeleteBtn.Click += new EventHandler(this.AreaDeleteBtn_Click);
            this.provinceAddBtn.Click += new EventHandler(this.provinceAddBtn_Click);
            this.catAddBtn.Click += new EventHandler(this.catAddBtn_Click);
            this.provinceDeleteBtn.Click += new EventHandler(this.provinceDeleteBtn_Click);
            this.catDeleteBtn.Click += new EventHandler(this.catDeleteBtn_Click);
            this.roleDdl.SelectedIndexChanged += new EventHandler(this.roleDdl_SelectedIndexChanged);
            this.SaveBtn.Click += new EventHandler(this.SaveBtn_Click);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.SetFrom();
            if (this.Principal.SysUser.IsAdmin == 2)
            {
                this.lbIsSuperAdmin.Visible = true;
                this.isSuperAdminChk.Visible = true;
            }
            else
            {
                this.lbIsSuperAdmin.Visible = false;
                this.isSuperAdminChk.Visible = false;
            }
            this.AreaDdl.Items.Clear();
            Area[] area = ManageArea.GetAreaAryViaId(this.Principal.SysUser.UserId);
            for (int i = 0; i < area.Length; i++)
            {
                ListItem tmpListItem = new ListItem();
                tmpListItem.Text = area[i].AreaName;
                tmpListItem.Value = area[i].AreaId.ToString();
                this.AreaDdl.Items.Add(tmpListItem);
            }
            this.provinceDdl.DataSource = ManageProvince.GetProvince(new Guid(this.AreaDdl.SelectedValue));
            this.provinceDdl.DataTextField = "RegionName";
            this.provinceDdl.DataValueField = "RegionId";
            this.provinceDdl.DataBind();
            this.catDdl.DataSource = ManageCAT.GetCAT(new Guid(this.AreaDdl.SelectedValue));
            this.catDdl.DataTextField = "CATGroupName";
            this.catDdl.DataValueField = "CATGroupId";
            this.catDdl.DataBind();
            this.roleDdl.DataSource = ManageRole.GetRole(new Guid(this.AreaDdl.SelectedValue));
            this.roleDdl.DataTextField = "RoleName";
            this.roleDdl.DataValueField = "RoleId";
            this.roleDdl.DataBind();
            if (base.Request.QueryString["IsModify"] == "True")
            {
                Guid id = new Guid(base.Request.QueryString["UserId"]);
                SysUser user = ManageUser.GetUserViaId(id);
                if ((ManageArea.GetAreaAryViaId(id).Length > 1) && (this.Principal.OwnArea.Length == 1))
                {
                    base.Response.Redirect("UserManage.aspx", true);
                }
                else
                {
                    this.userNameTbx.Text = user.UserName;
                    this.loginNameTbx.Text = user.LoginName;
                    this.userEmailTbx.Text = user.UserEmail;
                    for (int i = 0; i < this.roleDdl.Items.Count; i++)
                    {
                        if (this.roleDdl.Items[i].Value == user.RoleId.ToString())
                        {
                            this.roleDdl.SelectedIndex = i;
                        }
                    }
                    Province[] provinces = ManageProvince.GetProvinceAryViaUserId(id);
                    for (int i = 0; i < provinces.Length; i++)
                    {
                        ListItem tmpListItem = new ListItem();
                        tmpListItem.Text = provinces[i].ProvinceName;
                        tmpListItem.Value = provinces[i].ProvinceId.ToString();
                        this.provinceLB.Items.Add(tmpListItem);
                    }
                    Cat[] cat = ManageCAT.GetCATAryViaId(id);
                    for (int i = 0; i < cat.Length; i++)
                    {
                        ListItem tmpListItem = new ListItem();
                        tmpListItem.Text = cat[i].CatName;
                        tmpListItem.Value = cat[i].CatId.ToString();
                        this.catLB.Items.Add(tmpListItem);
                    }
                    this.AreaLB.Items.Clear();
                    Area[] areaEdit = ManageArea.GetAreaAryViaId(id);
                    for (int i = 0; i < areaEdit.Length; i++)
                    {
                        ListItem tmpListItem = new ListItem();
                        tmpListItem.Text = areaEdit[i].AreaName;
                        tmpListItem.Value = areaEdit[i].AreaId.ToString();
                        this.AreaLB.Items.Add(tmpListItem);
                    }
                    this.AreaDdl.Items.Clear();
                    Area[] areaEditInDdl = this.Principal.OwnArea;
                    for (int i = 0; i < areaEditInDdl.Length; i++)
                    {
                        ListItem tmpListItem = new ListItem();
                        tmpListItem.Text = areaEditInDdl[i].AreaName;
                        tmpListItem.Value = areaEditInDdl[i].AreaId.ToString();
                        this.AreaDdl.Items.Add(tmpListItem);
                    }
                    if (user.IsAdmin == 2)
                    {
                        this.isAdminChk.Checked = true;
                        this.isSuperAdminChk.Checked = true;
                    }
                    if (user.IsAdmin == 1)
                    {
                        this.isAdminChk.Checked = true;
                    }
                    if (user.IsDownload == 1)
                    {
                        this.isDownloadChk.Checked = true;
                    }
                }
            }
        }

        private bool IsExistItem(ListItem tmpItem, ListBox tmpList)
        {
            for (int i = 0; i < tmpList.Items.Count; i++)
            {
                if (tmpItem.Value == tmpList.Items[i].Value)
                {
                    return true;
                }
            }
            return false;
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (this.Principal.SysUser.IsAdmin == 0)
            {
                throw new ApplicationException(string.Format("User: {0} not has privilege to access this page.", this.Principal.SysUser.LoginName));
            }
            if (!base.IsPostBack)
            {
                this.InitPage();
            }
        }

        private void provinceAddBtn_Click(object sender, EventArgs e)
        {
            if (this.provinceDdl.SelectedItem.Value == "00000000-0000-0000-0000-000000000000")
            {
                this.provinceLB.DataSource = ManageProvince.GetProvince(new Guid(this.AreaDdl.SelectedValue));
                this.provinceLB.DataTextField = "RegionName";
                this.provinceLB.DataValueField = "RegionId";
                this.provinceLB.DataBind();
                try
                {
                    this.provinceLB.Items.Remove(this.provinceLB.Items[0]);
                }
                catch
                {
                }
            }
            else
            {
                ListItem tmpItem = new ListItem();
                tmpItem.Text = this.provinceDdl.SelectedItem.Text;
                tmpItem.Value = this.provinceDdl.SelectedItem.Value;
                if (!this.IsExistItem(tmpItem, this.provinceLB))
                {
                    this.provinceLB.Items.Add(tmpItem);
                }
            }
        }

        private void provinceDeleteBtn_Click(object sender, EventArgs e)
        {
            try
            {
                this.provinceLB.Items.Remove(this.provinceLB.Items[this.provinceLB.SelectedIndex]);
            }
            catch
            {
            }
        }

        private void roleDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((this.roleDdl.SelectedValue == "00000000-0000-0000-0000-000000000000") || (this.roleDdl.SelectedValue == "00000000-0000-0000-0000-0000000000001"))
            {
                this.isAdminChk.Checked = true;
            }
            else
            {
                this.isAdminChk.Checked = false;
            }
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            string userName = this.userNameTbx.Text.Trim();
            string userLoginName = this.loginNameTbx.Text.Trim();
            string userEmail = this.userEmailTbx.Text.Trim();
            Guid[] provinceId = new Guid[this.provinceLB.Items.Count];
            Guid[] catId = new Guid[this.catLB.Items.Count];
            Guid[] areaId = new Guid[this.AreaLB.Items.Count];
            if (((provinceId.Length == 0) || (catId.Length == 0)) || (areaId.Length == 0))
            {
                base.Response.Write("<script> alert('List中不能有空') </script>");
            }
            else
            {
                try
                {
                    for (int i = 0; i < this.provinceLB.Items.Count; i++)
                    {
                        provinceId[i] = new Guid(this.provinceLB.Items[i].Value);
                    }
                    for (int i = 0; i < this.catLB.Items.Count; i++)
                    {
                        catId[i] = new Guid(this.catLB.Items[i].Value);
                    }
                    for (int i = 0; i < this.AreaLB.Items.Count; i++)
                    {
                        areaId[i] = new Guid(this.AreaLB.Items[i].Value);
                    }
                }
                catch
                {
                    return;
                }
                if (base.Request.QueryString["IsModify"] == "True")
                {
                    if ((userLoginName != "") || (userName != ""))
                    {
                        int isAdmin = 0;
                        int isDownload = 0;
                        if ((this.roleDdl.SelectedValue == "00000000-0000-0000-0000-000000000000") || (this.roleDdl.SelectedValue == "00000000-0000-0000-0000-000000000001"))
                        {
                            isAdmin = 1;
                        }
                        if (this.isSuperAdminChk.Checked)
                        {
                            isAdmin = 2;
                        }
                        if (this.isDownloadChk.Checked)
                        {
                            isDownload = 1;
                        }
                        ManageUser.UpdateUser(userName, userLoginName, userEmail, new Guid(this.roleDdl.SelectedItem.Value), new Guid(base.Request.QueryString["UserId"]), isAdmin, isDownload, provinceId, catId, areaId);
                        base.Response.Write("<script>self.close();</script>");
                    }
                    else
                    {
                        base.Response.Write("<script> alert('Name ro LoginName is Null !') </script>");
                    }
                }
                else if (userLoginName != "")
                {
                    if (!ManageUser.IsExistSameUserName(userLoginName))
                    {
                        int isDownload = 0;
                        int isAdmin = 0;
                        if (this.isDownloadChk.Checked)
                        {
                            isDownload = 1;
                        }
                        if ((this.roleDdl.SelectedValue == "00000000-0000-0000-0000-000000000000") || (this.roleDdl.SelectedValue == "00000000-0000-0000-0000-000000000001"))
                        {
                            isAdmin = 1;
                        }
                        if (this.isSuperAdminChk.Checked)
                        {
                            isAdmin = 2;
                        }
                        if (isAdmin > 0)
                        {
                            ManageUser.InsertNewAdminUser(userName, userLoginName, userEmail, new Guid(this.roleDdl.SelectedItem.Value), isDownload, provinceId, catId, areaId, isAdmin);
                        }
                        else
                        {
                            ManageUser.InsertNewUser(userName, userLoginName, userEmail, new Guid(this.roleDdl.SelectedItem.Value), isDownload, provinceId, catId, areaId);
                        }
                        base.Response.Write("<script>self.close();</script>");
                    }
                    else
                    {
                        base.Response.Write("<script> alert('该用户已存在') </script>");
                    }
                }
            }
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/LanguageConfig.xml");
            this.lbuserName.Text = ManageSystemConfig.GetSystemSetLanguage("UserEdit", "lbuserName", language, path);
            this.lbloginName.Text = ManageSystemConfig.GetSystemSetLanguage("UserEdit", "lbloginName", language, path);
            this.lbuserEmail.Text = ManageSystemConfig.GetSystemSetLanguage("UserEdit", "lbuserEmail", language, path);
            if (this.Principal.OwnArea[0].AreaId.ToString() == "a7f26634-10a9-4cb1-aa2c-8c91a8dfdd66")
            {
                this.lbprovince.Text = ManageSystemConfig.GetSystemSetLanguage("UserEdit", "lbprovinceOverSea", language, path);
            }
            else
            {
                this.lbprovince.Text = ManageSystemConfig.GetSystemSetLanguage("UserEdit", "lbprovince", language, path);
            }
            this.lbRole.Text = ManageSystemConfig.GetSystemSetLanguage("UserEdit", "lbRole", language, path);
            this.lbIsAdmin.Text = ManageSystemConfig.GetSystemSetLanguage("UserEdit", "lbIsAdmin", language, path);
            this.lbisDownload.Text = ManageSystemConfig.GetSystemSetLanguage("UserEdit", "lbisDownload", language, path);
            this.SaveBtn.Text = ManageSystemConfig.GetSystemSetLanguage("UserEdit", "SaveBtn", language, path);
            this.lbIsSuperAdmin.Text = ManageSystemConfig.GetSystemSetLanguage("UserEdit", "lbIsSuperAdmin", language, path);
        }
    }
}

